<?php

namespace app\official\controller;

use think\Controller;
// 微信公众号封装类
use EasyWeChat\Factory;
// 微信公众号配置
use addons\wechat\library\Config;

class Template extends Controller
{


  protected $app = null;

  public function __construct()
  {
    // 手动继承父级的构造函数
    parent::__construct();

    // 初始化配置
    $this->app = Factory::officialAccount(Config::load());
  }

  public function send()
  {
    if ($this->request->isPost()) {
      $touser = $this->request->param('touser', '', 'trim');

      /* // 发送模板消息（给个人发送信息）
      $result = $this->app->template_message->send($data);

      if ($result) {
        echo "发送模板消息成功";
      } else {
        echo "发送模板消息失败";
      }
      $template_id = "9WTwHHEpph-GaCKPZXhhN-PsT7FQkpNyJmeSsAFjLlM";

      $data = [
        'touser' => $touser,
        'template_id' => $template_id,
        'url' => "http://gwsc.gjjun.top",
        "topcolor" => "#FF0000",
        "data" => []
      ]; */
      // 获取所有关注用户列表（全体发送消息）
      $list =  $this->app->user->list();

      $touserlist = $list['data']['openid'];

      $template_id = "9WTwHHEpph-GaCKPZXhhN-PsT7FQkpNyJmeSsAFjLlM";

      foreach ($touserlist as $item) {
        $data = [
          'touser' => $item,
          'template_id' => $template_id,
          'url' => "http://gwsc.gjjun.top",
          "topcolor" => "#FF0000",
          "data" => []
        ];

        // 发送模板消息
        $result = $this->app->template_message->send($data);

        if ($result) {
          echo "发送模板消息成功";
        } else {
          echo "发送模板消息失败";
        }
      }
    }
    return $this->view->fetch();
  }
}
